package financesV20240619

import (
	"encoding/json"
	"fmt"
	"gitee.com/HuaLiLiDeDiDiao/amazon-spapi-sdk-go/client"
	"net/url"
	"time"
)

// ListTransactions retrieves a list of financial transactions
func ListTransactions(spClient *client.SPAPIClient, request ListTransactionsRequest) (*ListTransactionsResponse, error) {
	path := "/finances/2024-06-19/transactions"

	queryParams := url.Values{}
	if !request.PostedAfter.IsZero() {
		queryParams.Add("postedAfter", request.PostedAfter.Format(time.RFC3339))
	}
	if !request.PostedBefore.IsZero() {
		queryParams.Add("postedBefore", request.PostedBefore.Format(time.RFC3339))
	}
	if request.MarketplaceId != "" {
		queryParams.Add("marketplaceId", request.MarketplaceId)
	}
	if request.NextToken != "" {
		queryParams.Add("nextToken", request.NextToken)
	}

	resp, err := spClient.MakeRequest("GET", path, queryParams, nil)
	if err != nil {
		return nil, err
	}
	var response ListTransactionsResponse
	if err := json.Unmarshal(resp.Body(), &response); err != nil {
		return nil, fmt.Errorf("failed to parse response: %w", err)
	}
	return &response, nil
}
